package com.example.UI;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.example.UI.database.MySQLDataHelper;
import com.example.ui.R;


public class InformationActivity extends AppCompatActivity {
    private TextView textViewClassName;
    private TextView textViewName;
    private TextView textViewStudentId;
    private TextView textViewUserName;
    private MySQLDataHelper mySQLDataHelper;
    private TextView mtextViewSet;
    private TextView mtextViewClass;
    private Button mButtonMain;
    private Button mButtonCourses;
    private Button buttonStudents;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_information);

        mySQLDataHelper = new MySQLDataHelper(this);


        String studentId = getIntent().getStringExtra("studentId");
        textViewClassName = findViewById(R.id.i_tx_class);
        textViewName = findViewById(R.id.i_tx_name);
        textViewStudentId = findViewById(R.id.i_tx_id);
        textViewUserName = findViewById(R.id.i_tx_username);
        fillProfileData(studentId);

        mtextViewSet =findViewById(R.id.i_tx_set);
        mtextViewSet.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent setIntent = new Intent(InformationActivity.this, SettingActivity.class);
                startActivity(setIntent);
            }
        });
        mtextViewClass =findViewById(R.id.i_tx_selcet);
        mtextViewClass.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent setIntent = new Intent(InformationActivity.this, SelectCourseActivity.class);
                startActivity(setIntent);
            }
        });

        buttonStudents = findViewById(R.id.buttonStudents);
        buttonStudents.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent courseIntent = new Intent(InformationActivity.this,StudentActivity.class);
                startActivity(courseIntent);
            }
        });

        mButtonCourses = findViewById(R.id.buttonCourses);
        mButtonCourses.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent courseIntent = new Intent(InformationActivity.this, CourseActivity.class);
                startActivity(courseIntent);
            }
        });
    }


    private void fillProfileData(String studentId) {
        String[] columns = {"username","class_name", "name", "student_id"};
        String selection = "student_id = ?";
        String[] selectionArgs = {studentId};

        SQLiteDatabase db = mySQLDataHelper.getReadableDatabase();
        Cursor cursor = null;
        try {
            cursor = db.query("users", columns, selection, selectionArgs, null, null, null);
            if (cursor.moveToFirst()) {
                String username = cursor.getString(cursor.getColumnIndex("username"));
                String className = cursor.getString(cursor.getColumnIndex("class_name"));
                String name = cursor.getString(cursor.getColumnIndex("name"));

                // 按照指定格式设置文本
                textViewUserName.setText("用户名：" + username);
                textViewClassName.setText("班级：" + className);
                textViewName.setText("学号：" + studentId);
                textViewStudentId.setText("姓名：" + name);
            }
        } catch (Exception e) {
            Log.e("InformationActivity", "Error querying user data", e);
        } finally {
            if (cursor != null) {
                cursor.close();
            }
            db.close();
        }
    }

}